複合型の構造
C++ 複合型 は基本型と 型修飾子を組み合わせることで定義される。参照とポインタの両方とも間接参照を提供するが、哲学的に異なる。 参照 (&) は永続的なエイリアス——既存のオブジェクトの別名である。一度バインドされると、再配置できない。逆に、 ポインタ (*) はメモリ上の独立したオブジェクトであり、16進数形式の アドレスを格納している。別のオブジェクトに再設定したり、 nullptrに設定することもできる。
メモリの可視化
コードでは int *p1, p2;において、 p1 はポインタである。 p2 は通常の整数である。両方をポインタにするには、 int *p1, *p2;を使用する。これは、修飾子は基本型ではなく個々の宣言子に属することを強調している。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates a reference correctly?
int &r;
int i = 10; int &r = i;
int &r = 10;
int *&r = &i;
✅ Correct!
Correct! References must be initialized and bound to an object, not a literal or nothing.❌ Incorrect
References are not objects; they must be bound to an existing variable upon declaration.QUESTION 2
In the declaration 'int* p1, p2;', what are the types of p1 and p2?
Both are pointers to int.
p1 is an int, p2 is a pointer to int.
p1 is a pointer to int, p2 is a plain int.
Neither are valid C++.
✅ Correct!
The '*' modifier applies only to p1. The base type is int, but the declarator p2 lacks the modifier.❌ Incorrect
Type modifiers like '*' or '&' attach to the specific variable name, not the base type (int).QUESTION 3
Which operator is used to obtain the memory address of an object?
The asterisk (*)
The ampersand (&)
The arrow (->)
The dot (.)
✅ Correct!
The address-of operator (&) returns the location of an object in memory.❌ Incorrect
The asterisk (*) is used for dereferencing (accessing the value at an address) or declaration.QUESTION 4
What is the result of dereferencing a pointer?
It gives the memory address.
It creates a new reference.
It yields the object to which the pointer points.
It clears the pointer's memory.
✅ Correct!
Dereferencing 'follows' the address to the actual object, allowing you to read or modify it.❌ Incorrect
Dereferencing retrieves the value, while the address-of operator retrieves the location.QUESTION 5
Which statement about references is FALSE?
A reference is not an object.
A reference can be null.
A reference must be initialized.
A reference cannot be redirected to a new object.
✅ Correct!
References must always be bound to a valid object; there is no such thing as a 'null reference'.❌ Incorrect
References are fixed aliases and do not have a null state, unlike pointers.Module Assessment: Pointers, References, and Sales_data
Applying Compound Types and Structs
You are tasked with upgrading a legacy system. You must manage data using custom structures and ensure memory safety through correct pointer and reference usage.
Q
Exercise 2.41: Based on the Sales_data struct (including bookNo, units_sold, and revenue), how would you rewrite a loop to read multiple transactions and update the total? Focus on the main function logic.
Solution:
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
Q
Consider the following snippet: 'int i = 42; int *p = &i; *p = *p * *p;'. Explain the final value of 'i' and the role of the asterisk in each part of the second line.
Solution:
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.